#include <PA9.h>
#include "graphic.h"
#include "sprite.h"
#include "spritetext.h"

namespace game{
    SpriteText::SpriteText(bool screen, void* character_pal, void* character_sprite, u8 objShape, u8 objSize, u8 maxFrames, u8 characterOffset){
        SpriteText::screen = screen;
        SpriteText::character_pal = character_pal;
        SpriteText::character_sprite = character_sprite;
        SpriteText::objShape = objShape;
        SpriteText::objSize = objSize;
        
        SpriteText::maxFrames = maxFrames;
        SpriteText::characterOffset = characterOffset;
    }
    
    SpriteText:: ~SpriteText(){
        delete[] char_spr;
        delete char_pal;
    }
    
    void SpriteText::load(char* text, u8 size, u8 characterSpacing){
        this->characterSpacing = characterSpacing;
        char_pal = new Palette(screen,(void*)character_pal);
        textSize = size-1;
        
        char_spr = new Sprite*[textSize];
    
        int chri;
        for(chri = 0; chri < textSize; chri++){
            Graphic* characters_gfx = new Graphic(screen, (void*)character_sprite, objShape, objSize, 1);
            char_spr[chri] = new Sprite(screen, char_pal, characters_gfx, maxFrames);
            char_spr[chri]->load(127-chri, chri*characterSpacing, 0);
            char_spr[chri]->setframe((u8)text[chri]-characterOffset);
            orig_x = 0;
            orig_y = 0;
        }
    }
    
    void SpriteText::settext(char* text){
        u8 chri;
        for(chri = 0; chri < textSize; chri++){
            char_spr[chri]->setframe((u8)text[chri]-characterOffset);
        }
    }
    
    void SpriteText::setxy(s16 x, s16 y){
        u8 chri;
        
        for(chri = 0; chri < textSize; chri++){
            u8 char_x = char_spr[chri]->getx() - orig_x;
            u8 char_y = char_spr[chri]->gety() - orig_y;
            char_x += x;
            char_y += y;
            char_spr[chri]->setxy(char_x,char_y);
        }
        orig_x = x;
        orig_y = y;
    }
    
    s16 SpriteText::getx(){
        return orig_x;
    }
    
    void SpriteText::setx(s16 x){
        setxy(x, gety());
    }
    
    s16 SpriteText::gety(){
        return orig_y;
    }
    
    void SpriteText::sety(s16 y){
        setxy(getx(), y);
    }
    
    void SpriteText::setxy(u8 chr, s16 x, s16 y){}
    
    s16 SpriteText::getx(u8 chr){
        return char_spr[chr]->getx();
    }
    
    void SpriteText::setx(u8 chr, s16 x){
        char_spr[chr]->setx(x + (chr * characterSpacing));
    }
    
    s16 SpriteText::gety(u8 chr){
        return char_spr[chr]->gety();
    }
    
    void SpriteText::sety(u8 chr, s16 y){
        char_spr[chr]->sety(y);
    }
}
